﻿using BZOrderPrint.BLL.Helper;
using BZOrderPrint.DAL;
using BZOrderPrint.DAL.Entity;
using BZOrderPrint.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BZOrderPrint
{
    public partial class FrmClientManage : Form
    {
        private PageModel PageModel { get; set; }

        private BZContext context { get; set; }

        private FrmShop shop { get; set; }


        public FrmClientManage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            context = new BZContext(ConfigHelper.ConnString);
        }

        private ClientPageModel GetShopPageModel(int PageIndex)
        {
            ClientPageModel model = new ClientPageModel();
            Expression<Func<BzClientEntity, bool>> predicate = x => true;
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                predicate = predicate.And(x => x.Name.Contains(textBox1.Text));
            }
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                predicate = predicate.And(x => x.Phone.Contains(textBox2.Text));
            }
            var count = context.bzClientEntities.Count(predicate.Compile());
            if (count > 0)
            {
                model.PageSize = 30;
                model.TotalRows = count;
                model.TotalPage = count % 30 == 0 ? count / 30 : count / 30 + 1;
                model.PageIndex = PageIndex;
                model.bzClientEntities = context.bzClientEntities.Where(predicate.Compile()).Skip((PageIndex - 1) * 30).Take(30).ToList();
            }
            else
            {
                model.TotalRows = 0;
                model.TotalPage = 0;
                model.PageIndex = PageIndex;
                model.PageSize = 30;
            }
            return model;
        }

        private void DataGirdViewInit(ClientPageModel model)
        {
            #region 初始化
            dataGridView1.Columns.Clear();
            var data = new DataTable();
            var dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "ID",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "客户名称",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "手机号",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "所属门店",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "地址",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "创建日期",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            
            #endregion 初始化

            shop = shop ?? new FrmShop();

            foreach (var item in model.bzClientEntities)
            {
                var shopName = shop.Shops.FirstOrDefault(x => x.Key.Equals(item.ShopId));
                var dataRow = data.NewRow();
                dataRow["ID"] = item.Id;
                dataRow["客户名称"] = item.Name;
                dataRow["手机号"] = item.Phone;
                dataRow["所属门店"] = shopName.Value;
                dataRow["地址"] = item.Address;
                dataRow["创建日期"] = item.CreateTime.ToShortDateString();
                
                data.Rows.Add(dataRow);
            }
            //DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn();
            //checkColum.HeaderText = "选择";
            //checkColum.Name = "IsChecked";
            //checkColum.DataPropertyName = "IsChecked";
            //checkColum.ReadOnly = false;
            //checkColum.TrueValue = true;
            //checkColum.FalseValue = false;
            //checkColum.Width = 40;
            //dataGridView1.Columns.Insert(0, checkColum);
            DataGridViewButtonColumn buttonColum = new DataGridViewButtonColumn();
            buttonColum.Text = "查看";
            buttonColum.Name = "查看";
            buttonColum.ToolTipText = "查看";
            buttonColum.ReadOnly = false;
            buttonColum.Width = 40;
            //dataGridView1.Columns.Insert(0, buttonColum);
            dataGridView1.DataSource = data;
            dataGridView1.ReadOnly = false;
            //dataGridView1.Columns[1].Visible = false;
            for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            pageIndex.Text = model.PageIndex.ToString();
        }

        private void btnSreach_Click(object sender, EventArgs e)
        {
            int i = 1;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }

            DataGirdViewInit(GetShopPageModel(i));
        }

        private void pageIndex_TextChanged(object sender, EventArgs e)
        {
            if (!int.TryParse(pageIndex.Text, out int index))
            {
                MessageBox.Show("请输入数字！");
                return;
            }
            else if (index < 1)
            {
                MessageBox.Show("请输入大于0的数字！");
                return;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }

            DataGirdViewInit(GetShopPageModel(++i));
        }

        private void FrmClientManage_Load(object sender, EventArgs e)
        {
            DataGirdViewInit(GetShopPageModel(1));
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }
            if (--i == 0)
                return;
            DataGirdViewInit(GetShopPageModel(--i));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmShopModify frmShopModify = new FrmShopModify(new BzShopEntity());
            frmShopModify.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //var id = 0;

            //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            //{
            //    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
            //    {
            //        if (id > 0)
            //        {
            //            MessageBox.Show("请选择一条数据修改!");
            //            return;
            //        }
            //        id = int.Parse(dataGridView1.Rows[i].Cells["ID"].Value.ToString());
            //        using (var db = new BZContext(ConfigHelper.ConnString))
            //        {
            //            var shop = db.bzShopEntities.FirstOrDefault(x => x.Id.Equals(id));
            //            FrmShopModify frmShopModify = new FrmShopModify(shop);
            //            frmShopModify.ShowDialog();
            //        }
            //    }
            //}
            //if (id == 0)
            //{
            //    MessageBox.Show("请选择一条数据修改!");
            //    return;
            //}
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                int id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                var shop = context.bzShopEntities.FirstOrDefault(x => x.Id.Equals(id));
                FrmShopModify frmShopModify = new FrmShopModify(shop);
                frmShopModify.ShowDialog();
            }
        }
    }
}